<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2007
sirfragalot@users.sourceforge.net

**********************************************************************************/
/*****
* BROWSEPUBLISHER class
*
* Browse publishers as a 'tag cloud'
*
* 
$Header: /cvsroot/wikindx/wikindx3/core/browse/BROWSEPUBLISHER.php,v 1.11 2007/07/20 10:38:09 sirfragalot Exp $
*****/
class BROWSEPUBLISHER
{
// Constructor
	function BROWSEPUBLISHER($db, $vars)
	{
		$this->db = $db;
		$this->vars = $vars;
		include_once("core/html/MISC.php");
		include_once("core/html/HTML.php");
		$this->html = new HTML();
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
		include_once("core/browse/BROWSECOMMON.php");
		$this->common = new BROWSECOMMON($this->db);
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
	}
// display publishers
	function display()
	{
		$this->template->setVar('heading', $this->messages->text("heading", "browsePublisher"));
		$this->sum = $this->publisher = array();
		$this->getPublishers();
		if(empty($this->publisher))
		{
			$pString = MISC::p("&nbsp;") . $this->messages->text("misc", "noPublishers");
			$this->template->setVar('body', $pString);
			return $this->template->process();
		}
		natcasesort($this->sum);
		natcasesort($this->publisher);
		$this->publisher = $this->common->paging($this->vars, $this->publisher);
		$bib = $this->bib ? MISC::p($this->bib, "hint") : FALSE;
		$pString = $bib . $this->common->linksInfo() . 
			MISC::pBrowse($this->process());
		$this->template->setVar('body', $pString);
		$this->template->setVar('paging', $this->common->pagingLinks("action=browsePublisher"));
		return $this->template->process();
	}
// Process and display results
	function process()
	{
		$lowestSum = current($this->sum);
		$highestSum = end($this->sum);
		foreach($this->publisher as $id => $name)
		{
//			$name = str_replace(' ', "&nbsp;", $name);
			$name = $this->html->codeToHtml($name);
			//$colour = $this->common->colourText($lowestSum, $highestSum, $this->sum[$id]);
			//$size = $this->common->sizeText($lowestSum, $highestSum, $this->sum[$id]);
			if(array_key_exists($id, $this->miscField1))
				$links[] = MISC::aBrowse($colour, $size, $name, "index.php?action=listConfPublisherProcess" . 
					htmlentities("&id=" . $id)) . "&nbsp;[" . $this->sum[$id] . "]";
			else
				$links[] = MISC::aBrowse($colour, $size, $name, "index.php?action=listPublisherProcess" . 
					htmlentities("&id=" . $id)) . "&nbsp;[" . $this->sum[$id] . "]";
		}
		return join("<br> ", $links);
	}
// Get publishers from db
	function getPublishers()
	{
		$this->miscField1 = array();
		list($condition, $bib) = $this->common->userBibCondition('WKX_resource_misc');
		if($condition)
			$condition = " WHERE " . $condition;
		$join = " LEFT JOIN " . $this->db->formatTable('WKX_resource') . " ON " . 
			$this->db->formatField('WKX_resource.id') . "=" . $this->db->formatField('WKX_resource_misc.id');
		$recordset = $this->db->select(array('WKX_resource_misc'), 
			array('WKX_resource_misc.id', 'publisher', 'miscField1', 'type'), $join . $condition);
		while($row = $this->db->loopRecordSet($recordset))
		{
// For proceedings_article, publsiher is stored in miscField1
			if(($row['type'] == 'proceedings_article') && $row['miscField1'])
			{
				$row['publisher'] = $row['miscField1'];
				$this->collate($row, TRUE);
			}
			else
				$this->collate($row, FALSE);
		}
		if($bib)
			$this->bib = $this->messages->text("user", "bibliography") . ": " . $bib;
		else if($this->session->getVar('setup_multiUser'))
			$this->bib = $this->messages->text("user", "bibliography") . ": " . 
				$this->messages->text("user", "masterBib");
		else
			$this->bib = FALSE;
	}
// Add publishers to array and sum totals
	function collate($row, $miscField1)
	{
		if(!$row['publisher'])
			return FALSE;
		if(!array_key_exists($row['publisher'], $this->sum))
			$this->sum[$row['publisher']] = 1;
		else
			++$this->sum[$row['publisher']];
		$publishersCondition = "'" . $row['publisher'] . "'";
		$condition = $this->db->formatField('id') . "=" . $publishersCondition;
		$recordset = $this->db->select(array('WKX_publisher'), 
			array("id", "publisherName", "publisherLocation"), 
			" WHERE " . $condition);
		while($row = $this->db->loopRecordSet($recordset))
		{
			if(array_key_exists('publisherName', $row) && array_key_exists('publisherLocation', $row) 
				&& $row['publisherName'] && $row['publisherLocation'])
				$this->publisher[$row['id']] = stripslashes($row['publisherName']) . 
				"&nbsp;(" . stripslashes($row['publisherLocation']) . ")";
			else if(array_key_exists('publisherLocation', $row) && $row['publisherLocation'])
				$this->publisher[$row['id']] = "(" . stripslashes($row['publisherLocation']) . ")";
			else
				$this->publisher[$row['id']] = preg_replace("/{(.*)}/U", "$1", 
				stripslashes($row['publisherName']));
			if($miscField1)
				$this->miscField1[$row['id']] = TRUE;
		}
	}
}
?>